package com.xuhuan.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuhuan.practice.dto.category.CategoryInfoDTO;
import com.xuhuan.practice.entity.Category;
import com.xuhuan.practice.mapper.CategoryMapper;
import com.xuhuan.practice.service.CategoryService;
import com.xuhuan.practice.utils.CurrentUserUtil;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * @author: huan.xu
 * @create: 2024/6/11 10:17
 * @description: 分类service接口实现类
 **/
@Service
public class CategoryServiceImpl extends
    ServiceImpl<CategoryMapper, Category> implements CategoryService {

  /**
   * 新增分类
   *
   * @param categoryInfoDTO
   */
  @Override
  public void add(CategoryInfoDTO categoryInfoDTO) {
    Category category = new Category();
    category.setCategoryName(categoryInfoDTO.getCategoryName());
    category.setCategoryAlias(categoryInfoDTO.getCategoryAlias());
    category.setCreateTime(LocalDateTime.now());
    category.setUpdateTime(LocalDateTime.now());
    category.setCreateUser(CurrentUserUtil.getCurrentUserId());
    saveOrUpdate(category);
  }

  /**
   * 文章分类列表查询
   *
   * @return
   */
  @Override
  public List<Category> list() {
    LambdaQueryWrapper<Category> lambdaQuery = Wrappers.lambdaQuery();
    lambdaQuery.eq(Category::getCreateUser, CurrentUserUtil.getCurrentUserId());
    return baseMapper.selectList(lambdaQuery);
  }

  /**
   * 根据id查询分类信息
   *
   * @param categoryId
   * @return
   */
  @Override
  public Category getById(Integer categoryId) {
    return baseMapper.selectById(categoryId);
  }

  /**
   * 根据id删除分类信息
   *
   * @param categoryId
   */
  @Override
  public void deleteById(Integer categoryId) {
    baseMapper.deleteById(categoryId);
  }

  /**
   * 更新文章分类
   *
   * @param category
   */
  @Override
  public void update(Category category) {
    category.setUpdateTime(LocalDateTime.now());
    saveOrUpdate(category);
  }
}
